package enum05;

import org.junit.Test;

import java.lang.reflect.Method;

/**
 * Java中的所有的自定义枚举都会默认继承一个类,也会继承该类中的方法:java.lang.Enum
 * Class类中方法:
 *   public native Class<? super T> getSuperclass():获取该字节码对象的直接父字节码对象
 *   public Method[] getMethods() throws SecurityException:获取指定的字节码中所有的公有方法
 * 枚举中常用的方法:
 *   从java.lang.Enum类中继承的:
 *        public static <T extends Enum<T>> T valueOf(Class<T> enumClass,
 *                                                String name):根据指定的枚举字节码,常量名获取该枚举常量
 *        public final String name(): 获取常量名
 *
 *   编译器添加的两个方法;
 *        public static T valueOf(String name):根据常量名获取枚举对象
 *        public static T[] values():将所有的枚举常量封装到一个数组中返回
 */
public class WeekDemo {
    /*验证自定义枚举的父类是java.lang.Enum*/
    @Test
    public void testMethod01(){
        System.out.println(Week.class.getSuperclass());//class java.lang.Enum
    }

    /*利用反射获取枚举中的所有公有方法*/
    @Test
    public void testMethod02(){
        Method[] methods = Week.class.getMethods();
        for (Method method : methods) {
            System.out.println(method);
        }
    }

    /*测试values方法*/
    @Test
    public void testMethod03(){
        Week[] weeks = Week.values();//相当于Week[] weeks = {MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY}
        for (Week week : weeks) {
            System.out.println(week);
        }
    }

    /*测试valueOf方法*/
    @Test
    public void testMethod04(){
        Week monday = Week.valueOf("MONDAY");
        System.out.println(monday);
        System.out.println(Week.MONDAY == Week.valueOf("MONDAY"));
    }

    /*测试valueOf方法*/
    @Test
    public void testMethod05(){
        Week monday = Week.valueOf(Week.class, "MONDAY");
        System.out.println(monday);
        System.out.println(Week.MONDAY == Week.valueOf(Week.class, "MONDAY"));
    }

    /*测试name()方法*/
    @Test
    public void testMethod06(){
        System.out.println(Week.MONDAY.name());//返回当前枚举常量的默认名称(常量名)
        System.out.println(Week.MONDAY.toString());//返回当前枚举常量的默认名称(常量名)
        System.out.println(Week.MONDAY);//返回当前枚举常量的默认名称(常量名)
    }





}
